package cn.liulin.spring_boot_study.config;

import cn.liulin.spring_boot_study.condition.DatabaseConditional;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * cn.liulin.spring_boot_study.config$
 *
 * @author ll
 * @date 2021-11-01 17:26:37
 **/
@Configuration
@PropertySource("classpath:jdbc.properties")
public class ConditionConfig {
    @Bean(name = "dataSource")
    @Conditional(DatabaseConditional.class)
    public DataSource getDataSource(@Value("${database.driverName}")String driver,
                                    @Value("${database.url}")String url,
                                    @Value("${database.username}")String username,
                                    @Value("${database.password}")String password) {
        Properties pro = new Properties();
        pro.setProperty("driver", driver);
        pro.setProperty("url", url);
        pro.setProperty("username", username);
        pro.setProperty("password", password);
        DataSource dataSource = null;
        try {
            dataSource = BasicDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }
}
